/**
 * Created by 駿濤 on 2016/11/17 0017.
 */

$(function () {
    bind_img_del_btn_mouse();
});

function bind_img_del_btn_mouse() {
    $('.layui-layer-phimg').unbind('mouseover');
    $('.layui-layer-phimg').mouseover(function () {
        $(this).find('.img-del-btn').show();
    });
    $('.layui-layer-phimg').unbind('mouseout');
    $('.layui-layer-phimg').mouseout(function () {
        $(this).find('.img-del-btn').hide();
    });
}

function ajax_result(res) {
    if (res.code == 1) {
        toast.success(res.msg);
        if (res.url) {
            setTimeout(function () {
                if (res.url == 'reload') {
                    location.reload();
                } else {
                    location.href = res.url;
                }
            }, res.wait * 1000)
        }
    } else {
        toast.error(res.msg);
    }
}

function up_attach_val(obj, type, attachId) {
    var attachVal = obj.val();
    var attachArr = attachVal.split(',');
    var newArr = [];
    for (var i in attachArr) {
        if (attachArr[i] !== '' && attachArr[i] !== attachId.toString()) {
            newArr.push(attachArr[i]);
        }
    }
    type === 'add' && newArr.push(attachId);
    obj.val(newArr.join(','));
    return newArr;
}


function get_url_params() {
    var url = location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            var sTemp = strs[i].split("=");
            theRequest[sTemp[0]] = (sTemp[1]);
        }
    }
    return theRequest;
}

function build_query(params) {
    var query = '';
    for (var k in params) {
        query += ((query.indexOf("=") != -1) ? "&" : "") + k + "=" + encodeURI(params[k]);
    }

    return query;
}

function go_to_page(page) {
    var params = get_url_params();
    params.page = page;
    location.href = location.href.split('?')[0] + '?' + build_query(params);
}


$(function () {
    $(document).on('submit', 'form.ajax-form', function (e) {
        e.preventDefault();
        //禁用提交按钮，防止重复提交
        var form = $(this);
        var action = form.attr('action');
        var method = form.attr('method');
        //检测提交地址
        if (!action) {
            return false;
        }
        //默认提交方式为get
        if (!method) {
            method = 'get';
        }
        //获取表单内容
        var formContent = form.serialize();
        //发送提交请求
        var callable;
        if (method == 'post') {
            callable = $.post;
        } else {
            callable = $.get;
        }
        callable(action, formContent, function (result) {
            ajax_result(result);
        });
        //返回
        return false;
    })


    $('[data-role="ajax-post"]').click(function () {
        var url = $(this).attr('data-url');
        $.post(url, {id: $(this).closest('tr').find('[name="ids"]').val()}, function (res) {
            ajax_result(res);
        })
    })

    $('[data-role="redirect"]').click(function () {
        var url = $(this).attr('data-url');
        var id = $(this).closest('tr').find('[name="ids"]').val();
        location.href = url + (url.indexOf('?') != -1 ? '&' : '?') + "id=" + id;
    })

});


var toast = {
    success: function (str) {
        layui.layer.msg(str, {icon: 6});
    },
    error: function (str) {
        layui.layer.msg(str, {icon: 5});
    },
    showLoading: function () {
        layui.layer.load();
    },
    hideLoading: function () {
        layui.layer.closeAll('loading');
    },
    alert: function (str) {
        layui.layer.alert(str);
    }

};


function get_url_params() {
    var url = location.search;
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            var sTemp = strs[i].split("=");
            theRequest[sTemp[0]] = (sTemp[1]);
        }
    }
    return theRequest;
}

function build_query(params) {
    var query = '';
    for (var k in params) {
        query += ((query.indexOf("=") != -1) ? "&" : "") + k + "=" + encodeURI(params[k]);
    }
    return query;
}

function go_to_page(page) {
    var params = get_url_params();
    params.page = page;
    location.href = location.href.split('?')[0] + '?' + build_query(params);
}